<?php

namespace App\V3\V3_2_6\Controllers;

use App\V3\V3_2_6\Models\TallyModel;
use App\Helper\Utils\TimeUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class TallyController extends BaseController
{
    /**
     * 记账列表
     * @param Request $request
     * AUTHOR  : Chen
     * DateTime: 2022/3/19 10:08
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type' => [
                'required',
                'integer',
                Rule::In([0, 1, 2]),
            ],//收支类型：0-收入 1-支出 2-全部
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
            'type.required' => '参数`type`缺失',
            'type.integer' => '参数`type`仅能为数字',
            'type.in' => '参数`type`不合法',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $condition[] = ['main_id','=',$this->getMainUserId()];
        if($data['type']!=2){
            $condition[] = ['type','=',$data['type']];
        }
        //查询
        $model = TallyModel::where($condition)
            ->where('delete_flag', 0)
            ->orderBy('id','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
        return $this->success($model);
    }

    /**
     * 详细
     * @param Request $request
     * AUTHOR  : Chen
     * DateTime: 2022/3/19 10:09
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//记账分类主键id
        ],[
            'id.required' => '记账标识id缺失',
            'id.integer' => '记账标识id仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $model = TallyModel::find($data['id']);
        return $this->success($model);
    }

    /**
     * 新建
     * @param Request $request
     * AUTHOR  : Chen
     * DateTime: 2022/3/19 10:17
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'tally_class_name' =>'required',//记账类型名称
            'type' => [
                'required',
                'integer',
                Rule::In([0, 1]),
            ],//收支类型：0-收入 1-支出
            'money' => 'numeric'
        ],[
            'tally_class_name.required' => '请填写记账类型名称',
            'type.required' => '请选择收支类型',
            'type.integer' => '您选择的收支类型不合法，请重试',
            'type.in' => '您选择的收支类型不合法，请重试',
            'money.numeric' => '金额仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $data['main_id'] = $this->getMainUserId();
        $data['created_at'] = strtotime(date("Y-m-d",time()));
        $data['add_time'] = time();
        $data['proof_image'] = empty($data['proof_image'])?'':$data['proof_image'];
        $data['order_no'] = 'HM'.$data['main_id'].TimeUtil::getMillisecond();
        $id = TallyModel::insertGetId($data);
        $this->log('添加记账',$data);
        return $this->success($id);
    }

    /**
     * 编辑
     * @param Request $request
     * AUTHOR  : Chen
     * DateTime: 2022/3/19 10:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//记账分类主键id
            'tally_class_name' =>'required',//记账类型名称
            'type' => [
                'required',
                'integer',
                Rule::In([0, 1]),
            ],//收支类型：0-收入 1-支出
            'money' =>'numeric',//  记账金额
        ],[
            'id.required' => '记账分类主键id缺失',
            'id.integer' => '记账分类主键id异常',
            'tally_class_name.required' => '请填写记账类型名称',
            'type.required' => '请选择收支类型',
            'type.integer' => '您选择的收支类型不合法，请重试',
            'type.in' => '您选择的收支类型不合法，请重试',
            'money.numeric' => '金额仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('',$info);
        }
        TallyModel::where('id',$data['id'])->update($data);
        $this->log('编辑记账',$data);
        return $this->success();
    }

    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required',//记账分类主键id   多条用逗号分隔
        ],[
            'id.required' => '记账分类主键id缺失'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        TallyModel::destroy(explode(',',$data['id']));
        $this->log('删除记账',$data);
        return $this->success();
    }
}
